using UnityEngine;
using System.Collections;

public class SidescrollGameplayArea : MonoBehaviour
{
	#region Variables
		public	Vector3				m_v3Size;
		public	Vector3				m_v3OldPos; 
		public	Vector3				m_v3OldRot; 
		public	Vector3				m_v3RotSpeed; 
		public	Vector3				m_v3Speed; 
		public	GameplayAreaDecoy	m_tGameplayAreaDecoy;
		public	Transform			m_tOrienter;
		private	bool				m_bOrientingShips	=	false;
	#endregion	
	
	public Vector3 v3Speed
	{
		get
		{
			return m_v3Speed;
		}
		set
		{
			m_v3Speed		=	value;
		}
	}
	
	public	bool bOrientingShips
	{
		get
		{
			return	m_bOrientingShips;
		}
		set
		{
			m_bOrientingShips	=	value;
		}
	}
	
	void Start()
	{
		m_v3OldPos	=	transform.position;
	}
	
	void Update()
	{
		m_v3Speed	=	(transform.position - m_v3OldPos) / Time.deltaTime;
		m_v3RotSpeed=	(transform.rotation.eulerAngles - m_v3OldRot) / Time.deltaTime;
		m_v3OldPos	=	transform.position;
		m_v3OldRot	=	transform.rotation.eulerAngles;
	}
			
	#region Attaching and deattaching
		public void DeattachBullets()
		{
			// Create a decoy and set it running
			GameplayAreaDecoy tGameplayAreaDecoy = GameObject.Instantiate(m_tGameplayAreaDecoy
		                                                              	, transform.position
		                                                              	, transform.rotation)
																		as GameplayAreaDecoy;
			tGameplayAreaDecoy.Velocity 		= m_v3Speed;
			tGameplayAreaDecoy.TorqueVelocity 	= m_v3RotSpeed;
			
			// Find all bullets attached and reattach them
			foreach(Transform bBullet in transform)
			{
				if(bBullet.gameObject.GetComponent("Bullet"))
				{
					bBullet.transform.parent = tGameplayAreaDecoy.transform; 
				}
			}
		}
	#endregion
	
	#region Functions to place objects
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public void PlaceOnItFromRelativePos(Transform _tMe, ref Vector2 _v2LocalPos)
		{
			_tMe.position	=	transform.position 
							+ (	transform.right 	*	_v2LocalPos.x) 
							+ (	transform.up 		*	_v2LocalPos.y);
		}
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public void GetPositionOnItFromRelativePos(Transform _tMe, ref Vector2 _v2LocalPos)
		{
			_tMe.position	=	transform.position 
							+ (	transform.right 	*	_v2LocalPos.x) 
							+ (	transform.up 		*	_v2LocalPos.y);
		}
		
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public void ProjectOnItFromWorld(Transform _tMe)
		{
			Vector3	v3RelativePos	= transform.InverseTransformPoint(_tMe.transform.position);
			v3RelativePos.z 		= 0;
			_tMe.position			= GetWorldPositionOnItFromRelativePos(ref v3RelativePos);
		}
	#endregion
	
	#region Functions to know where an objet is in gameplay area coords or vice-versa
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public void LimitRelativePositionInside(ref Vector3 _v3RelativePosition)
		{
			_v3RelativePosition.x = Mathf.Clamp(_v3RelativePosition.x, -m_v3Size.x, m_v3Size.x);
			_v3RelativePosition.y = Mathf.Clamp(_v3RelativePosition.y, -m_v3Size.y, m_v3Size.y);
			_v3RelativePosition.z = Mathf.Clamp(_v3RelativePosition.z, -m_v3Size.z, m_v3Size.z);
		}
	
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public void LimitRelativePositionInside(ref Vector2 _v2RelativePosition)
		{
			_v2RelativePosition.x = Mathf.Clamp(_v2RelativePosition.x, -m_v3Size.x, m_v3Size.x);
			_v2RelativePosition.y = Mathf.Clamp(_v2RelativePosition.y, -m_v3Size.y, m_v3Size.y);
		}
	
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public Vector3 GetWorldPositionOnItFromRelativePos(ref Vector2 _v2LocalPos)
		{
			return 		transform.position 
					+ (	transform.right 	*	_v2LocalPos.x) 
					+ (	transform.up 		*	_v2LocalPos.y);
		}
		public Vector3 GetWorldPositionOnItFromRelativePos(ref Vector3 _v3LocalPos)
		{
			return 		transform.position 
					+ (	transform.right 	*	_v3LocalPos.x) 
					+ (	transform.up 		*	_v3LocalPos.y)  
					+ (	transform.forward 	*	_v3LocalPos.z);
		}
		
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public Vector3 GetRelativePositionFromWorld(ref Vector3 _v3WorldPos)
		{
			Vector3 v3PlanePos; 
			v3PlanePos = transform.InverseTransformPoint(_v3WorldPos);
			return v3PlanePos;
		}
		
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public Vector2 GetRelativePositionFromWorldProjected(ref Vector3 _v3WorldPos)
		{
			Vector3 v3PlanePos; 
			v3PlanePos = transform.InverseTransformPoint(_v3WorldPos);
			return new Vector2(v3PlanePos.x, v3PlanePos.y);
		}
	
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public Vector3 GetRelativePositionInside(ref Vector3 _v3RelativePosition)
		{
			Vector3 v3ClampedPosition;
			v3ClampedPosition.x = Mathf.Clamp(_v3RelativePosition.x, -m_v3Size.x, m_v3Size.x);
			v3ClampedPosition.y = Mathf.Clamp(_v3RelativePosition.y, -m_v3Size.y, m_v3Size.y);
			v3ClampedPosition.z = Mathf.Clamp(_v3RelativePosition.z, -m_v3Size.z, m_v3Size.z);
			return v3ClampedPosition;
		}
	
		//---------------------------------------------------------------------------------------------------------
		//---------------------------------------------------------------------------------------------------------
		public Vector3 GetWorldPositionInside(ref Vector3 _v3WorldPosition)
		{
			Vector3 v3ClampedPosition	= GetRelativePositionFromWorld(ref _v3WorldPosition);
			v3ClampedPosition.x = Mathf.Clamp(v3ClampedPosition.x, -m_v3Size.x, m_v3Size.x);
			v3ClampedPosition.y = Mathf.Clamp(v3ClampedPosition.y, -m_v3Size.y, m_v3Size.y);
			v3ClampedPosition.z = Mathf.Clamp(v3ClampedPosition.z, -m_v3Size.z, m_v3Size.z);
			return GetWorldPositionOnItFromRelativePos(ref v3ClampedPosition);
		}
	#endregion
}

